package com.shop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.entity.*;
import com.shop.service.MessageBoardService;
import com.shop.service.PhoneService;
import com.shop.service.UserOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-06
 */
@RestController
@RequestMapping("/message-board")
public class MessageBoardController {
    @Autowired
    MessageBoardService messageBoardService;

    @Autowired
    UserOrderService userOrderService ;

    @Autowired
    PhoneService phoneService;
    @GetMapping("list")
    public Res list(String phoneId) {
        QueryWrapper<MessageBoard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_id",phoneId).eq("status",MessageBoardService.PASS);
        List<MessageBoard> list = messageBoardService.list(queryWrapper);
        return Res.success(list);
    }
    @GetMapping("listAll")
    public Res listAll(String phoneId) {
        QueryWrapper<MessageBoard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_id",phoneId);
        List<MessageBoard> list = messageBoardService.list(queryWrapper);

        return Res.success(list);
    }
    @PostMapping("save")
    public Res save (@RequestBody HashMap<String ,String> params) {
        String userName = params.get("userName");
        String phoneId = params.get("phoneId");
        String comment = params.get("comment");
        String orderId = params.get("orderId");
        String rate = params.get("rate");
        UserOrder order = new UserOrder();
        Phone phone = phoneService.getById(phoneId);
        order.setStatus(UserOrderService.COMMENT);
        boolean update = userOrderService.update(order, new QueryWrapper<UserOrder>().eq("id", orderId));
        MessageBoard messageBoard = new MessageBoard();
        messageBoard.setStatus(MessageBoardService.UNPASS);
        messageBoard.setMsg(comment);
        messageBoard.setCreateTime(new Date());
        messageBoard.setPhoneId(phoneId);
        messageBoard.setUserName(userName);
        messageBoard.setCreateTime(new Date());
        messageBoard.setId(UUID.randomUUID().toString());
        messageBoard.setCoverSrc(phone.getCoverSrc());
        messageBoard.setRate(rate);
        boolean save = messageBoardService.save(messageBoard);
        return update && save ? Res.success() : Res.error();
    }

    @PostMapping("pass")
    public Res pass(@RequestBody HashMap<String,String> params){
        String id = params.get("id");
        MessageBoard messageBoard = new MessageBoard();
        messageBoard.setStatus(MessageBoardService.PASS);
        boolean update = messageBoardService.update(messageBoard, new QueryWrapper<MessageBoard>().eq("id", id));
        return update ? Res.success() : Res.error();
    }

    @GetMapping("getByUserName")
    public Res getByUserName(String userName) {
       List<MessageBoardHistory> messageBoardHistory  =  messageBoardService.getByUserName(userName);
       return Res.success(messageBoardHistory);
    }

    @DeleteMapping("del")
    public Res del(@RequestBody HashMap<String,String> params) {
        String id = params.get("id");
        return messageBoardService.removeById(id) ? Res.success() :Res.error();
    }
}

